<template>
  <div>
    <avue-crud
      :data="dataList"
      :option="option"
      :page="page"
      @refresh-change="getDataList"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="getDataList"
    >
      <template slot-scope="scope" slot="menu">
        <el-button
          v-if="scope.row.isUpload == 0"
          type="text"
          size="small"
          icon="el-icon-upload"
          @click="openUploadDialog(scope.row)"
          >上传投标文件
        </el-button>
        <el-button
          v-else
          type="text"
          size="small"
          icon="el-icon-arrow-down"
          @click="revoke(scope.row)"
          >撤销
        </el-button>
      </template>
      <template slot-scope="scope" slot="type">
        <el-tag type="danger" size="small">{{ scope.label }}</el-tag>
      </template>
      <template slot-scope="props" slot="expand">
        <pre class="code">
        {{ props.row.stack }}
      </pre
        >
      </template>
    </avue-crud>
    <el-dialog
      title="上传投标文件"
      :visible.sync="uploadDialogVisible"
      width="40%"
      @close="reset('uploadForm')"
    >
      <span>
        <el-form
          :model="uploadForm"
          ref="uploadForm"
          :rules="uploadFormRules"
          label-width="120px"
          :inline="false"
          size="normal"
        >
          <el-form-item label="关联申请内容">
            <el-input disabled v-model="uploadForm.glSpName"></el-input>
          </el-form-item>
          <el-form-item label="投标文件">
            <el-upload
              ref="upload"
              :headers="headers"
              accept=".rar"
              :on-change="handleFileChange"
              :on-remove="removeFileList"
              :on-success="uploadSuccess"
              :auto-upload="true"
              :before-upload="checkFile"
              :http-request="upload"
              multiple
              :limit="1"
              :file-list="fileList"
            >
              <el-button
                slot="trigger"
                size="small"
                :disabled="fileList.length > 0"
                type="primary"
                >选择文件</el-button
              >
              <!-- <el-button style="margin-left: 10px;" size="small" type="success" @click="upload">上传</el-button> -->
              <div slot="tip" class="el-upload__tip">只允许上传RAR压缩文件</div>
            </el-upload>

            <!-- <el-upload
              name="multipartFile"
              class="upload-demo"
              drag
              action=""
              multiple
              accept=".rar"
              :on-success="uploadSuccess"
              :before-upload="checkFile"
              :http-request="upload"
              :file-list="fileList"
              :on-remove="removeFileList"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                将文件拖到此处，或<em>点击上传</em>
              </div>
              <div slot="tip" class="el-upload__tip">只允许上传RAR压缩文件</div>
            </el-upload> -->
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer">
        <el-button @click="uploadDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { baseUrl } from "@/config/env";
import request from "@/router/axios";
import { getStore } from "@/util/store";

export default {
  name: "errLogs",
  data() {
    return {
      queryForm: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      dataList: [],

      option: {
        menu: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        page: true,
        border: true,
        expand: false,
        refreshBtn: true,
        headerAlign: "center",
        searchShow: true,
        searchMenuSpan: 6,
        column: [
          {
            label: "招标单号",
            prop: "spNo",
            width: 180,
            align: "center",
            solt: false,
            search: true,
          },
          {
            label: "备案编号",
            width: 200,
            prop: "beianNo",
            overHidden: true,
          },
          {
            label: "关联申请内容",
            prop: "glSpName",
            overHidden: true,
            align: "center",
            solt: false,
            search: true,
          },
          {
            label: "招标方式",
            prop: "zbfs",
            hide: true,
          },
          {
            label: "挂网时间",
            align: "center",
            prop: "gwsj",
            width: 200,
          },
          {
            label: "报名开始时间",
            align: "center",
            prop: "signStartTime",
            width: 200,
          },
          {
            label: "报名结束时间",
            align: "center",
            prop: "signEndTime",
            width: 200,
          },
        ],
      },
      uploadDialogVisible: false,
      uploadForm: {
        zczxId: "",
        glSpName: "",
        fileUrl: "",
      },
      fileList: [],
      headers: { "Content-Type": "multipart/form-data" },
    };
  },
  created() {
    this.getDataList();
  },
  mounted() {},
  computed: {},
  props: [],
  methods: {
    getDataList() {
      let requestUrl =
        baseUrl +
        "/api/blade-ztb/tendermzczx/page?current=" +
        this.page.currentPage +
        "&pageSize=" +
        this.page.pageSize;
      request({
        url: requestUrl,

        method: "get",
        params: this.queryForm,
      })
        .then((res) => {
          console.log("=res=", res);
          if (res.data.code == 200) {
            this.dataList = res.data.data.records;
            this, (this.page.total = res.data.data.total);
            console.log("=dataList =", this.dataList);
          }
        })
        .catch((e) => {
          console.error(e);
        });
    },
    revoke(row) {
      console.log("==row==", row);
      const requestUrl = baseUrl + "/api/blade-ztb/tbrecords/revoke/" + row.id;
      request({
        url: requestUrl,
        method: "get",
      })
        .then((res) => {
          if (res.status == 200) {
            this.$message.success("撤销成功");
            this.getDataList();
          } else {
            this.$message.warning("撤销失败");
          }
        })
        .catch((e) => {});
    },

    openUploadDialog(row) {
      const requestUrl =
        baseUrl + "/api/blade-ztb/tendermzczx/checkCanUpload/" + row.id;
      request({
        url: requestUrl,
        method: "get",
      })
        .then((res) => {
          if (res.data.code == 200) {
            if (res.data.data) {
              console.log("=openUploadDialog==", row);
              this.uploadForm.zczxId = row.id;
              this.uploadForm.glSpName = row.glSpName;
              console.log("==getStor==", getStore({ name: "token" }));
              this.uploadDialogVisible = true;
            }
          }
        })
        .catch((e) => {
          console.error(e);
        });
    },

    save() {
      const requestUrl = baseUrl + "/api/blade-ztb/tbrecords/save";
      request({
        url: requestUrl,
        method: "post",
        data: this.uploadForm,
      })
        .then((res) => {
          console.log("==res==", res);
          if (res.status == 200) {
            this.$message.success("数据保存成功");
            this.reset("uploadForm");
            this.fileList = [];
            this.getDataList();
            this.uploadDialogVisible = false;
          } else {
            this.$message.warning("数据保存失败");
          }
        })
        .catch((e) => {
          console.error(e);
        });
    },
    searchReset() {
      this.queryForm = {};
      this.getDataList();
    },
    searchChange(params) {
      this.queryForm = Object.assign(params, this.queryForm);
      console.log("=searchChange==queryForm==", this.queryForm);
      this.getDataList();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getDataList();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getDataList();
    },

    handleFileChange(file, fileList) {
      console.info("=handleFileChange=file=fileList=", file, fileList);
      this.fileList = fileList;
    },
    checkFile(file) {
      console.info("=file=", file);
      var filename = file.name;
      var index1 = filename.lastIndexOf(".");
      var index2 = filename.length;
      var type = filename.substring(index1, index2);
      if (type != ".rar" && type != ".RAR") {
        this.$message.info("格式不支持");
        return false;
      }
      console.info("=fileList=", this.fileList);
      this.$message.info("准备上传");
    },
    upload(file) {
      let uploadFile = file.file;
      console.log("==uploadFile==", uploadFile);
      const fd = new FormData();
      fd.append("file", uploadFile);
      console.log("==fdt==", fd);
      let uploadUrl = baseUrl + "/api/blade-ztb/upload/document";
      const config = { headers: { "Content-Type": "multipart/form-data" } };
      axios
        .post(uploadUrl, fd, config)
        .then((res) => {
          if (res.data.code == 200) {
            this.uploadForm.fileUrl = res.data.data;
            this.$message.success("文件上传成功");
          } else {
            this.$message.error("文件上传失败");
          }
        })
        .catch((error) => {
          console.error(error);
        });
    },
    uploadSuccess() {
      console.log("==uploadSuccess==");
    },
    removeFileList(file, fileList) {
      console.info("=file=fileList=", file, fileList);
      this.fileList = [];
    },
    reset(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.code {
  font-size: 12px;
  display: block;
  font-family: monospace;
  white-space: pre;
  margin: 1em 0px;
}
</style>
